home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15799 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  93 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Constructor member initializer list
  5. Message-ID: <marnoldDpHEuD.EGy@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4jua8k$fa1$2@mhadg.production.compuserve.com>
  8. Date: Sun, 7 Apr 1996 07:44:37 GMT
  9. Sender: marnold@netcom19.netcom.com
  10.  
  11. Alan Huff <74312.2300@CompuServe.COM> writes:
  12.  
  13. >How do you initialize class members in the initializer list if the 
  14. >member is a structure?
  15.  
  16. >Consider the following code fragment.
  17.  
  18. >typedef struct _tagRect {
  19. >   int      x;
  20. >   int      y;
  21. >   int      w;
  22. >   int      h;
  23. >} RECTANGLE;
  24.  
  25. This style of struct declaration is not necessary is C++.  The following
  26. will do...
  27.  
  28. struct RECTANGLE {
  29.    int      x;
  30.    int      y;
  31.    int      w;
  32.    int      h;
  33. };
  34.  
  35. In C++, structs are equivalent to classes, except everything defaults 
  36. to public.  structs can even have member functions, etc..
  37.  
  38. The typedef and _tagRect nonsense is not needed in C++.
  39.  
  40. >class Foo {
  41. >   Foo() : <how do I initialize fooRect and it's members> {};
  42. >   RECTANGLE   fooRect;
  43. >};
  44.  
  45. >Any suggestions would be greatly appreciated.
  46.  
  47. Give RECTANGLE a constructor that takes four parameters to intialize x, 
  48. y, w and h with...
  49.  
  50. struct RECTANGLE {
  51.    int      x;
  52.    int      y;
  53.    int      w;
  54.    int      h;
  55.  
  56.    RECTANGLE(int X, int Y, int W, int H): x(X), y(Y), w(W), h(H) { } 
  57. };
  58.  
  59. ...and then you'll be able to write Foo like so...
  60.  
  61. class Foo {
  62.    Foo(): fooRect(1, 2, 3, 4) { }
  63.    RECTANGLE fooRect;
  64. };
  65.  
  66. If RECTANGLE must be declared "C-style", as you showed above (because 
  67. it's also used with C code in your project, for example, or part of 
  68. some C library you are using) and so can't give it a constructor or 
  69. other member functions, you'll have to intialize fooRect outside of 
  70. Foo's intializer list...
  71.  
  72. class Foo {
  73.    Foo() {
  74.       fooRect.x = 1;
  75.       fooRect.y = 2;
  76.       fooRect.w = 3;
  77.       fooRect.h = 4;
  78.    }
  79.    RECTANGLE fooRect;
  80. };
  81.  
  82. You can only intialize types that have constructors in an intializer 
  83. list.
  84.  
  85. Regards,
  86. -------------------------------------------------------------------------
  87. Matt Arnold                       |        | ||| | |||| |  | | || ||
  88. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  89. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  90. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  91. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  92. -------------------------------------------------------------------------
  93.